package cn.magictodo.weblog.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

/**
 * JSON工具类相关
 *
 * @author liu
 * @time 2023-11-14 00:23
 */

@Slf4j
public class JsonUtil {
//    静态的ObjectMapper类
    private static final ObjectMapper INSTANCE = new ObjectMapper();

    /**将传入对象打印成json字符
     *
     * @param obj
     * @return json字符串
     */
    public static String toJsonString(Object obj) {
        try {
            return INSTANCE.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            return obj.toString();
        }
    }
}
